<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height:60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="hAdd"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="list">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template v-slot="{row}">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="small" type="primary" @click="hChange(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="hDel(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!-- 分页组件 -->
              <el-pagination
                :current-page="q.page"
                :page-sizes="[2, 3, 5, 10]"
                :page-size="q.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <el-dialog
        :title="isEidt ? '编辑' : '添加' "
        :is-eidt="false"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="showDialog"
        @closed="hClosed"
      >
        <el-form ref="roleForm" :model="roleForm" :rules="rules" label-width="100px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="roleForm.name" />
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input v-model="roleForm.description" />
          </el-form-item>
        </el-form>
        <!-- 底部 -->
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" @click="showDialog=false">取消</el-button>
            <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getRoles, delRoles, getUserRoles, postUserRoles, putUserRoles } from '@/api/roles'
export default {
  data() {
    return {
      list: [],
      q: {
        page: 1,
        pagesize: 5
      },
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }
        ]
        // description: [
        //   { required: true, message: '请输入角色名称', trigger: 'blur' }
        // ]
      },
      total: 0,
      showDialog: false,
      isEidt: false
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    // 、渲染数据
    async loadRoles() {
      try {
        const res = await getRoles(this.q)
        // console.log(res)
        this.list = res.data.rows
        this.total = res.data.total
      } catch (e) {
        console.log(e)
      }
    },
    // 删除
    hDel(id) {
      this.$confirm('确定删除吗?', '提示', { type: 'warning' })
        .then(async() => {
          try {
            if (this.list.length === 1 && this.q.page > 1) {
              this.q.page--
            }
            const res = await delRoles(id)
            // console.log(res)
            this.$message.success(res.message)
            this.loadRoles()
          } catch (e) {
            this.$message.warning(e.message)
          }
        }).catch(() => {})
    },
    handleSizeChange(pagesize) {
      this.q.pagesize = pagesize
      this.loadRoles()
    },
    handleCurrentChange(page) {
      this.q.page = page
      this.loadRoles()
    },
    // 编辑  数据回流
    async hChange(id) {
      this.isEidt = true
      this.showDialog = true
      try {
        const res = await getUserRoles(id)
        // console.log(res)
        this.roleForm = res.data
      } catch (e) {
        this.$message.error(e.message)
      }
    },
    // 添加
    hAdd() {
      this.showDialog = true
      this.isEidt = false
    },
    // 提交
    hSubmit() {
      this.$refs.roleForm.validate((valid) => {
        if (!valid) return
        this.isEidt ? this.doEidt() : this.doAdd()
      })
    },
    async doAdd() {
      try {
        const res = await postUserRoles(this.roleForm)
        console.log(res)
        this.$message.success(res.message)
        if (this.total % this.q.pagesize === 0) {
          this.total++
        }
        // 用总数除以当前页的数量，然后向上取整
        this.q.page = Math.ceil(this.total / this.q.pagesize)
        this.showDialog = false
        this.loadRoles()
      } catch (e) {
        this.$message.error(e.message)
      }
    },
    async doEidt() {
      try {
        await putUserRoles(this.roleForm)
        this.showDialog = false
        this.loadRoles()
      } catch (e) {
        this.$message.error(e.message)
      }
    },
    hClosed() {
      this.roleForm = {
        name: '',
        description: ''
      }
      this.$refs.roleForm.resetFields()
    }
  }
}
</script>
